#coding=utf-8
#Boa:Dialog:DlgBackupManager



import wx



from product import *
#import log # put here will cause error: logger is None, so 'import log' latter

from backup import *

import myLogger


def create(parent):
    return DlgBackupManager(parent)

[wxID_DLGBACKUPMANAGER, wxID_DLGBACKUPMANAGERBTNCANCEL, 
 wxID_DLGBACKUPMANAGERBTNOK, wxID_DLGBACKUPMANAGERBTNSETBACKUPPATH, 
 wxID_DLGBACKUPMANAGERBTNSETRECOVERYFILE, wxID_DLGBACKUPMANAGERBTNSTARTBACKUP, 
 wxID_DLGBACKUPMANAGERBTNSTARTRECOVERY, wxID_DLGBACKUPMANAGERCBBACKUPQUIT, 
 wxID_DLGBACKUPMANAGERCBEMAILAUTO, wxID_DLGBACKUPMANAGERGAUGEBACKUP, 
 wxID_DLGBACKUPMANAGERGAUGERECOVERY, wxID_DLGBACKUPMANAGERNOTEBOOK1, 
 wxID_DLGBACKUPMANAGERPANELBACKUP, wxID_DLGBACKUPMANAGERPANELRECOVERY, 
 wxID_DLGBACKUPMANAGERSTATICTEXT1, wxID_DLGBACKUPMANAGERSTATICTEXT2, 
 wxID_DLGBACKUPMANAGERSTATICTEXT3, wxID_DLGBACKUPMANAGERSTATICTEXT4, 
 wxID_DLGBACKUPMANAGERSTATICTEXT5, wxID_DLGBACKUPMANAGERSTATICTEXT6, 
 wxID_DLGBACKUPMANAGERTCBACKUPPATH, wxID_DLGBACKUPMANAGERTCCODE, 
 wxID_DLGBACKUPMANAGERTCCODECONFIRM, wxID_DLGBACKUPMANAGERTCEMAIL, 
 wxID_DLGBACKUPMANAGERTCPASSWORD, wxID_DLGBACKUPMANAGERTCRECOVEYFILE, 
] = [wx.NewId() for _init_ctrls in range(26)]

class DlgBackupManager(wx.Dialog):
    def _init_coll_notebook1_Pages(self, parent):
        # generated method, don't edit

        parent.AddPage(imageId=-1, page=self.panelBackup, select=True,
              text=u'\u5907\u4efd\u6570\u636e')
        parent.AddPage(imageId=-1, page=self.panelRecovery, select=False,
              text=u'\u6062\u590d\u6570\u636e')

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGBACKUPMANAGER,
              name=u'DlgBackupManager', parent=prnt, pos=wx.Point(508, 241),
              size=wx.Size(612, 408), style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u5907\u4efd\u4e0e\u6062\u590d')
        self.SetClientSize(wx.Size(604, 374))
        self.Center(wx.BOTH)

        self.notebook1 = wx.Notebook(id=wxID_DLGBACKUPMANAGERNOTEBOOK1,
              name='notebook1', parent=self, pos=wx.Point(16, 16),
              size=wx.Size(560, 264), style=0)

        self.panelBackup = wx.Panel(id=wxID_DLGBACKUPMANAGERPANELBACKUP,
              name=u'panelBackup', parent=self.notebook1, pos=wx.Point(0, 0),
              size=wx.Size(552, 237), style=wx.TAB_TRAVERSAL)

        self.panelRecovery = wx.Panel(id=wxID_DLGBACKUPMANAGERPANELRECOVERY,
              name=u'panelRecovery', parent=self.notebook1, pos=wx.Point(0, 0),
              size=wx.Size(552, 237), style=wx.TAB_TRAVERSAL)

        self.staticText1 = wx.StaticText(id=wxID_DLGBACKUPMANAGERSTATICTEXT1,
              label=u'\u5907\u4efd\u8def\u5f84\uff1a', name='staticText1',
              parent=self.panelBackup, pos=wx.Point(16, 32), size=wx.Size(60,
              14), style=0)

        self.tcBackupPath = wx.TextCtrl(id=wxID_DLGBACKUPMANAGERTCBACKUPPATH,
              name=u'tcBackupPath', parent=self.panelBackup, pos=wx.Point(80,
              24), size=wx.Size(344, 22), style=0, value=u'')

        self.btnSetBackupPath = wx.Button(id=wxID_DLGBACKUPMANAGERBTNSETBACKUPPATH,
              label=u'\u4fee\u6539\u8def\u5f84', name=u'btnSetBackupPath',
              parent=self.panelBackup, pos=wx.Point(432, 24), size=wx.Size(75,
              24), style=0)
        self.btnSetBackupPath.Bind(wx.EVT_BUTTON, self.OnBtnSetBackupPathButton,
              id=wxID_DLGBACKUPMANAGERBTNSETBACKUPPATH)

        self.tcEmail = wx.TextCtrl(id=wxID_DLGBACKUPMANAGERTCEMAIL,
              name=u'tcEmail', parent=self.panelBackup, pos=wx.Point(320, 88),
              size=wx.Size(192, 22), style=0, value=u'')

        self.cbBackupQuit = wx.CheckBox(id=wxID_DLGBACKUPMANAGERCBBACKUPQUIT,
              label=u'\u7cfb\u7edf\u9000\u51fa\u65f6\u81ea\u52a8\u5907\u4efd',
              name=u'cbBackupQuit', parent=self.panelBackup, pos=wx.Point(16,
              64), size=wx.Size(144, 14), style=0)
        self.cbBackupQuit.SetValue(True)
        self.cbBackupQuit.Bind(wx.EVT_CHECKBOX, self.OnCbBackupQuitCheckbox,
              id=wxID_DLGBACKUPMANAGERCBBACKUPQUIT)

        self.cbEmailAuto = wx.CheckBox(id=wxID_DLGBACKUPMANAGERCBEMAILAUTO,
              label=u'\u81ea\u52a8\u5907\u4efd\u5230\u7535\u5b50\u4fe1\u7bb1',
              name=u'cbEmailAuto', parent=self.panelBackup, pos=wx.Point(248,
              64), size=wx.Size(136, 14), style=0)
        self.cbEmailAuto.SetValue(True)
        self.cbEmailAuto.Bind(wx.EVT_CHECKBOX, self.OnCbEmailAutoCheckbox,
              id=wxID_DLGBACKUPMANAGERCBEMAILAUTO)

        self.btnOk = wx.Button(id=wxID_DLGBACKUPMANAGERBTNOK,
              label=u'\u786e\u5b9a', name=u'btnOk', parent=self,
              pos=wx.Point(168, 312), size=wx.Size(75, 24), style=0)
        self.btnOk.Bind(wx.EVT_BUTTON, self.OnBtnOkButton,
              id=wxID_DLGBACKUPMANAGERBTNOK)

        self.btnCancel = wx.Button(id=wxID_DLGBACKUPMANAGERBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(304, 312), size=wx.Size(75, 24), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGBACKUPMANAGERBTNCANCEL)

        self.btnStartBackup = wx.Button(id=wxID_DLGBACKUPMANAGERBTNSTARTBACKUP,
              label=u'\u624b\u52a8\u5907\u4efd', name=u'btnStartBackup',
              parent=self.panelBackup, pos=wx.Point(16, 192), size=wx.Size(75,
              24), style=0)
        self.btnStartBackup.Bind(wx.EVT_BUTTON, self.OnBtnStartBackupButton,
              id=wxID_DLGBACKUPMANAGERBTNSTARTBACKUP)

        self.staticText3 = wx.StaticText(id=wxID_DLGBACKUPMANAGERSTATICTEXT3,
              label=u'\u6062\u590d\u6587\u4ef6\uff1a', name='staticText3',
              parent=self.panelRecovery, pos=wx.Point(16, 32), size=wx.Size(60,
              14), style=0)

        self.tcRecoveyFile = wx.TextCtrl(id=wxID_DLGBACKUPMANAGERTCRECOVEYFILE,
              name=u'tcRecoveyFile', parent=self.panelRecovery, pos=wx.Point(80,
              24), size=wx.Size(384, 22), style=0, value=u'')

        self.btnSetRecoveryFile = wx.Button(id=wxID_DLGBACKUPMANAGERBTNSETRECOVERYFILE,
              label=u'\u5b9a\u4f4d\u6587\u4ef6', name=u'btnSetRecoveryFile',
              parent=self.panelRecovery, pos=wx.Point(472, 24), size=wx.Size(75,
              24), style=0)
        self.btnSetRecoveryFile.Bind(wx.EVT_BUTTON,
              self.OnBtnSetRecoveryFileButton,
              id=wxID_DLGBACKUPMANAGERBTNSETRECOVERYFILE)

        self.btnStartRecovery = wx.Button(id=wxID_DLGBACKUPMANAGERBTNSTARTRECOVERY,
              label=u'\u5f00\u59cb\u6062\u590d', name=u'btnStartRecovery',
              parent=self.panelRecovery, pos=wx.Point(16, 80), size=wx.Size(75,
              24), style=0)
        self.btnStartRecovery.Bind(wx.EVT_BUTTON, self.OnBtnStartRecoveryButton,
              id=wxID_DLGBACKUPMANAGERBTNSTARTRECOVERY)

        self.gaugeBackup = wx.Gauge(id=wxID_DLGBACKUPMANAGERGAUGEBACKUP,
              name=u'gaugeBackup', parent=self.panelBackup, pos=wx.Point(112,
              192), range=100, size=wx.Size(232, 24), style=wx.GA_HORIZONTAL)
        self.gaugeBackup.Show(False)

        self.gaugeRecovery = wx.Gauge(id=wxID_DLGBACKUPMANAGERGAUGERECOVERY,
              name=u'gaugeRecovery', parent=self.panelRecovery,
              pos=wx.Point(104, 80), range=100, size=wx.Size(184, 24),
              style=wx.GA_HORIZONTAL)
        self.gaugeRecovery.Show(False)

        self.staticText2 = wx.StaticText(id=wxID_DLGBACKUPMANAGERSTATICTEXT2,
              label=u'\u8d26\u53f7\uff1a', name='staticText2',
              parent=self.panelBackup, pos=wx.Point(264, 88), size=wx.Size(36,
              14), style=0)

        self.staticText4 = wx.StaticText(id=wxID_DLGBACKUPMANAGERSTATICTEXT4,
              label=u'\u5bc6\u7801\uff1a', name='staticText4',
              parent=self.panelBackup, pos=wx.Point(264, 120), size=wx.Size(36,
              14), style=0)

        self.tcPassword = wx.TextCtrl(id=wxID_DLGBACKUPMANAGERTCPASSWORD,
              name=u'tcPassword', parent=self.panelBackup, pos=wx.Point(320,
              120), size=wx.Size(100, 22), style=wx.TE_PASSWORD, value=u'')

        self.staticText5 = wx.StaticText(id=wxID_DLGBACKUPMANAGERSTATICTEXT5,
              label=u'\u5907\u4efd\u5bc6\u7801\uff1a', name='staticText5',
              parent=self.panelBackup, pos=wx.Point(32, 88), size=wx.Size(60,
              14), style=0)

        self.staticText6 = wx.StaticText(id=wxID_DLGBACKUPMANAGERSTATICTEXT6,
              label=u'\u5bc6\u7801\u786e\u8ba4\uff1a', name='staticText6',
              parent=self.panelBackup, pos=wx.Point(32, 120), size=wx.Size(60,
              14), style=0)

        self.tcCode = wx.TextCtrl(id=wxID_DLGBACKUPMANAGERTCCODE,
              name=u'tcCode', parent=self.panelBackup, pos=wx.Point(96, 88),
              size=wx.Size(128, 22), style=wx.TE_PASSWORD, value=u'')

        self.tcCodeConfirm = wx.TextCtrl(id=wxID_DLGBACKUPMANAGERTCCODECONFIRM,
              name=u'tcCodeConfirm', parent=self.panelBackup, pos=wx.Point(96,
              120), size=wx.Size(128, 22), style=wx.TE_PASSWORD, value=u'')

        self._init_coll_notebook1_Pages(self.notebook1)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.myInit()
        
    def myInit(self):
        self.Bind(wx.EVT_TIMER, self.TimerHandler)
        self.timer = wx.Timer(self)

        
        backupCfg = CDataBackup.getBackupConfig()
        
        dir = backupCfg.getFieldVal(index=CDataBackup.COL_PATH)
        dir = dir.replace('/', '\\')
        self.tcBackupPath.SetValue(dir)
        
        if (backupCfg.getFieldVal(index=CDataBackup.COL_BACKUPQUIT) == 1):
            self.cbBackupQuit.SetValue(True)
            self.tcCode.Enable()
            self.tcCodeConfirm.Enable()
        else:
            self.cbBackupQuit.SetValue(False)
            self.tcCode.Enable(False)
            self.tcCodeConfirm.Enable(False)
            
        self.tcCode.SetValue(backupCfg.getFieldVal(index=CDataBackup.COL_CODE))
        self.tcCodeConfirm.SetValue(backupCfg.getFieldVal(index=CDataBackup.COL_CODE))
        
        if (backupCfg.getFieldVal(index=CDataBackup.COL_AUTOEMAIL) == 1):
            self.cbEmailAuto.SetValue(True)
            self.tcEmail.Enable()
            self.tcPassword.Enable()
        else:
            self.cbEmailAuto.SetValue(False)
            self.tcEmail.Enable(False)
            self.tcPassword.Enable(False)         
            
        self.tcEmail.SetValue(backupCfg.getFieldVal(index=CDataBackup.COL_EMAIL))
        self.tcPassword.SetValue(backupCfg.getFieldVal(index=CDataBackup.COL_PSW))
        
        file = backupCfg.getFieldVal(index=CDataBackup.COL_LASTFILE)
        file = file.replace('/', '\\')
        self.tcRecoveyFile.SetValue(file)
        
    def OnBtnSetRecoveryFileButton(self, event):
        file = self.tcRecoveyFile.GetValue()
        dlg = wx.FileDialog(self, u'导入文件', defaultFile = file)
        if (dlg.ShowModal() == wx.ID_OK):
            myLogger.logger.info(u'选择导入文件：%s', dlg.GetPath())
            self.tcRecoveyFile.SetValue(dlg.GetPath())
            
    def OnBtnSetBackupPathButton(self, event):
        '''重设备份文件路径'''
        curDir = self.tcBackupPath.GetValue()
        dlg = wx.DirDialog(self, u'备份路径', curDir)
        if (dlg.ShowModal() == wx.ID_OK):
            myLogger.logger.info(u'修改备份文件路径：%s', dlg.GetPath())
            self.tcBackupPath.SetValue(dlg.GetPath())

    def saveToDB(self):
        backupCfg = CDataBackup.getBackupConfig()
        data = {}
        file = self.tcRecoveyFile.GetValue()
        data[CDataBackup.COL_LASTFILE] = file.replace('\\', '/')
        data[CDataBackup.COL_PSW] = self.tcPassword.GetValue()
        data[CDataBackup.COL_EMAIL] = self.tcEmail.GetValue()
        if (self.cbEmailAuto.GetValue()):
            data[CDataBackup.COL_AUTOEMAIL] = 1
        else:
            data[CDataBackup.COL_AUTOEMAIL] = 0
            
        if (self.cbBackupQuit.GetValue()):
            data[CDataBackup.COL_BACKUPQUIT] = 1
        else:
            data[CDataBackup.COL_BACKUPQUIT] = 0
            
         
        path = self.tcBackupPath.GetValue()
        data[CDataBackup.COL_PATH] = path.replace('\\', '/')
        data[CDataBackup.COL_CODE] = self.tcCode.GetValue()
    
        backupCfg.setMulFieldVal(data)
        backupCfg.update()
        
    def OnBtnOkButton(self, event):
        '''保存当前配置'''
        if (self.tcCode.GetValue() != self.tcCodeConfirm.GetValue()):
            dlg = wx.MessageDialog(self, u'备份密码不一致，请重新输入', 
                            u'提示', wx.OK | wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        self.saveToDB()
        
        self.Close()

    def OnBtnCancelButton(self, event):
        self.Close()
                
    def OnBtnStartBackupButton(self, event):
        '''开始备份数据'''
        self.gauge = self.gaugeBackup
        #self.gauge.Pulse()
        self.timer.Start(100)

        #手动备份也要保存数据，因为要使用数据库中的信息
        self.saveToDB()
        
        dir = self.tcBackupPath.GetValue()
        if (dir == ''):
            dlg = wx.MessageDialog(self, u'数据备份错误，没有选择文件存放路径', 
                            u'提示', wx.OK | wx.ICON_ERROR)
            dlg.ShowModal()
            return
    
        retCode,msg,file = BackupManager.makeBackup(dir)
        if (retCode == BackupManager.SUCCEED):
            style = wx.OK | wx.ICON_INFORMATION
        else:
            style = wx.OK | wx.ICON_ERROR
            
        dlg = wx.MessageDialog(self, msg, u'提示', style)
        dlg.ShowModal()
        
        #更新最近备份文件
        if (file != ''):
            self.tcRecoveyFile.SetValue(file)
        
    def OnBtnStartRecoveryButton(self, event):
        '''开始恢复数据'''
        self.gauge = self.gaugeRecovery
        #self.gauge.Pulse()
        self.timer.Start(100)
    
        tgzFile = self.tcRecoveyFile.GetValue()
        if (tgzFile == ''):
            dlg = wx.MessageDialog(self, u'没有选择导入文件', 
                            u'提示', wx.OK | wx.ICON_ERROR)
            dlg.ShowModal()
            return
        
        dlg = wx.TextEntryDialog(self, u'输入解压密码', u'数据恢复', '', wx.OK|wx.CANCEL|wx.TE_PASSWORD)
        if (dlg.ShowModal() != wx.ID_OK):
            return
        
        psw = dlg.GetValue()
        
        retCode,msg = BackupManager.loadBackup(tgzFile, psw)
        if (retCode == BackupManager.SUCCEED):
            style = wx.OK | wx.ICON_INFORMATION
        else:
            style = wx.OK | wx.ICON_ERROR
            
        dlg = wx.MessageDialog(self, msg, u'提示', style)
        dlg.ShowModal()
        
    def TimerHandler(self, event):
        self.gauge.Pulse()

    def OnCbEmailAutoCheckbox(self, event):
        if (self.cbEmailAuto.GetValue()):
            self.tcPassword.Enable()
            self.tcEmail.Enable()
        else:
            self.tcPassword.Enable(False)
            self.tcEmail.Enable(False)

    def OnCbBackupQuitCheckbox(self, event):
        if (self.cbBackupQuit.GetValue()):
            self.tcCode.Enable()
            self.tcCodeConfirm.Enable()
        else:
            self.tcCode.Enable(False)
            self.tcCodeConfirm.Enable(False)
            
            

